package yx.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.VehicleServiceInt;
import yx.vo.JsonVO;
import yx.vo.VehicleVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "vehicleAction")
public class VehicleAction extends BaseAction implements ModelDriven<VehicleVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(VehicleAction.class);

	private VehicleVO vehicle = new VehicleVO();

	@Override
	public VehicleVO getModel() {
		return vehicle;
	}

	private VehicleServiceInt vehicleService;

	public VehicleServiceInt getVehicleService() {
		return vehicleService;
	}

	@Autowired
	public void setVehicleService(VehicleServiceInt vehicleService) {
		this.vehicleService = vehicleService;
	}

	public void queryVehicle() {
		/*
		 * TODO：需要调查为什么加判定
		 */
		if (vehicle.getStudentId() != null) {
			super.writeJson(vehicleService.datagrid(vehicle));
		}
	}

	public void addVehicle() {
		JsonVO j = new JsonVO();
		try {
			VehicleVO u = vehicleService.save(vehicle);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeVehicle() {
		vehicleService.remove(vehicle.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editVehicle() {
		VehicleVO u = vehicleService.edit(vehicle);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
